<?php
namespace app\user\controller;
use think\Request;
use think\Exception;
use app\common\controller\Common;
use app\user\model\User;
use think\Db;
class Visit extends Common
{
	protected $model = null;

	public function _initialize()
	{
		parent::_initialize();
		$this->model = new \app\user\model\Visit;
	} 
	/**
	* 跟单/拜访列表
	*/
	public function index($ty = null, $user_id = null)
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				return $this->get_index();
			}
			$user = User::get($user_id);
			$this->assign('user',$user);
			$view_hm = $this->viewHm($ty);
			return $this->fetch($view_hm);
		}
	}
	/**
	 * 拜访/跟单记录
	 */
	public function get_index()
	{
		$data = Request()->param('');
		return $this->getList($data);
	}
	/**
	* 记录详情
	*/
	public function details($ids = null)
	{
		$row = $this->model->get($ids);
		if(Request()->isGet()){
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	* 客户拜访名单
	*/
	public function visitList()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				return $this->get_visit_list();
			}
			return $this->fetch('');
		}
	}
	/**
	 * 客户拜訪名单获取
	 */
	public function get_visit_list()
	{
		$data = Request()->param('');
		$user = new \app\user\model\User;
		$this->order = 'ti.visit_t desc';

		$page = isset($data['page']) ? $data['page'] : 1;
		$row = isset($data['limit']) ? $data['limit'] : $this->row;
		$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
		$search = $this->keyword_search($search_arr);
		$order = $this->order;
		$where = $this->handleVisit($data);
		
		$list = $user->userVisit($where, $order, $page, $row, $search);
		$count = $user->userVisitCount($where, $order, $search);
		return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
	}
	/**
	* 客户跟单名单
	*/
	public function orderRoster()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				return $this->get_order_roster();
			}
			return $this->fetch('');
		}
	}
	/**
	 * 客户跟单名单获取
	 */
	public function get_order_roster()
	{
		return 0;
		
		$data = Request()->param('');
		$user = new \app\user\model\User;
		$this->order = 'ti.visit_t desc';

		$page = isset($data['page']) ? $data['page'] : 1;
		$row = isset($data['limit']) ? $data['limit'] : $this->row;
		$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
		$search = $this->keyword_search($search_arr);
		$order = $this->order;
		$where = $this->handleOrder($data);
		
		$list = $user->userOrder($where, $order, $page, $row, $search);
		$count = $user->userOrderCount($where, $order, $search);
		return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
	}
	/**
	* 添加拜访/跟单记录
	*/
	public function add($ty = null)
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$data = Request()->param('');
			try{
				Db::startTrans();
				$this->model->user_id = $data['user_id'];
				$this->model->admin_id = session('admin.admin_id');
				$this->model->content = $data['content'];
				$this->model->state = $ty;
				$this->model->addtime = time();
				$this->model->save();
				$user = new \app\user\model\User;
				$row = $user->get($data['user_id']);
				$row->visit_time = intval(strval($row->visit_t_text)) + 3600*24;
				$row->save();
				Db::commit();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				Db::rollback();
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}

	/************************************* 私有 **************************************/

	/*
	* 跟单/拜访列表
	* 数据处理
	*/
	protected function handleList($data)
	{
		$where['state'] = $data['ty'];
		/*@自定义任务*/
		if($data['ty'] == 2){
			$where['addtime'] = 0;
		}else{
			/*@时间*/
			$cusTime = $this->cusTime($data);
			if($cusTime === false) return false;
			if(is_array($cusTime)) $where['addtime'] = $cusTime;
		}
		/*@客户*/
		if(isset($data['user_id']) && $data['user_id'] != ''){
			$where['user_id'] = $data['user_id'];
		}
		return $where;
	}
	/**
	* 页面处理
	*/
	private function viewHm($ty)
	{
		switch($ty){
			case 0: return 'o_index';break;//跟单
			case 1: return 'b_index';break;//拜访
			default: $this->error(config('status.ERROR_SIX'));break;
		}
	}
	/**
	* 客户拜访名单
	* 数据处理
	*/
	private function handleVisit($data)
	{
		$where = [];
		/*@设置周期*/
		$where['u.visit'] = ['>',0];
		/*@开始时间*/
		$where['u.visit_time'] = [['>',0],['<=',strtotime(date('Y-m-d',time()))]];
		/*@时间*/
		$cusTime = $this->cusTime($data);
		if($cusTime === false) return false;
		if($cusTime){
			$where['ti.visit_t'] = $cusTime;
		}
		return $where;
	}
	/**
	* 客户跟单名单
	* 数据处理
	*/
	private function handleOrder($data)
	{
		$where = [];
		/*@时间*/
		$cusTime = $this->cusTime($data);
		if($cusTime === false) return false;
		if($cusTime){
			$where['or.order_time'] = $cusTime;
		}
		return $where;
	}

}